> PHP trim() 函数

PHP trim() 函数

定义和用法

trim() 函数从字符串的两端删除空白字符和其他预定义字符。

语法

trim(string,charlist)
参数 描述
string 必需。规定要检查的字符串。
charlist 可选。规定要转换的字符串。如果省略该参数,则删除以下所有字符:
  • "" - NULL
  • " " - tab
  • " " - new line
  • "x0B" - 纵向列表符
  • " " - 回车
  • " " - 普通空白字符

例子

例子 1

<html>
<body>
<?php
$str = "    Hello World!    ";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>
<body>
<html>

输出:

Without trim: Hello World!
With trim: Hello World!

HTML 源码:

<html>
<body>
Without trim:     Hello World!    <br />With trim: Hello World!
</body>
</html>

例子 2

<?php
$str = "
Hello World!
";
echo "Without trim: " . $str;
echo "<br />";
echo "With trim: " . trim($str);
?>

输出:

Without trim: Hello World!
With trim: Hello World!

HTML 源码:

<html>
<body>
Without trim: 
Hello World!
<br />With trim: Hello World!
</body>
</html>
参考手册

W3c0.com 提供的内容仅用于培训。我们不保证内容的正确性。通过使用本站内容随之而来的风险与本站无关。W3c0 简体中文版的所有内容仅供测试,对任何法律问题及风险不承担任何责任。 当使用本站时,代表您已接受了本站的使用条款和隐私条款。版权所有,保留一切权利。 鲁ICP备15022115号